package com.itheima.controller;

import com.itheima.pojo.Result;
import com.itheima.utils.AliOssUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController// 标记为控制器类,并且返回json数据
public class FileUploadController {

    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws Exception {


        // 获取文件的原始名字
        String originalFilename = file.getOriginalFilename();

        // 保证文件的名字是唯一的，从而防止文覆盖
        String fileName = UUID.randomUUID().toString()+"_"+originalFilename.substring(originalFilename.lastIndexOf("."));
//        file.transferTo(new File("F:\\files\\"+fileName));

        String url = AliOssUtil.uploadFile(fileName, file.getInputStream());


        return Result.success(url);

    }



}
